﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using GSdk.Net.Lglcd;
using System.Windows;
using System.Windows.Markup;
using System.Xaml;
using GSdk.Shared.Windows.Properties;

namespace GSdk.Shared.Windows
{
    public class Canvas : Layout, IDrawable
    {
        public Canvas()
            : base()
        {
        }

        public static Property<int> TopProperty = new AttachedProperty<Canvas, int>(() => TopProperty, new FrameworkPropertyMetadata(default(int), FrameworkPropertyMetadataOptions.Inherits));
        public static Property<int> LeftProperty = new AttachedProperty<Canvas, int>(() => LeftProperty, new FrameworkPropertyMetadata(default(int), FrameworkPropertyMetadataOptions.Inherits));
        public static Property<int> BottomProperty = new AttachedProperty<Canvas, int>(() => BottomProperty, new FrameworkPropertyMetadata(default(int), FrameworkPropertyMetadataOptions.Inherits));
        public static Property<int> RightProperty = new AttachedProperty<Canvas, int>(() => RightProperty, new FrameworkPropertyMetadata(default(int), FrameworkPropertyMetadataOptions.Inherits));

        public static void SetTop(DependencyObject obj, int value)
        {
            obj.SetValue(TopProperty, value);
        }
        public static int GetTop(DependencyObject obj)
        {
            return (int)obj.GetValue(TopProperty);
        }
        public static bool IsSetTop(DependencyObject obj)
        {
            return ((int)obj.GetValue(TopProperty) != default(int));
        }

        public static void SetLeft(DependencyObject obj, int value)
        {
            obj.SetValue(LeftProperty, value);
        }
        public static int GetLeft(DependencyObject obj)
        {
            return (int)obj.GetValue(LeftProperty);
        }
        public static bool IsSetLeft(DependencyObject obj)
        {
            return ((int)obj.GetValue(LeftProperty) != default(int));
        }

        public static void SetBottom(DependencyObject obj, int value)
        {
            obj.SetValue(BottomProperty, value);
        }
        public static int GetBottom(DependencyObject obj)
        {
            return (int)obj.GetValue(BottomProperty);
        }
        public static bool IsSetBottom(DependencyObject obj)
        {
            return ((int)obj.GetValue(BottomProperty) != default(int));
        }

        public static void SetRight(DependencyObject obj, int value)
        {
            obj.SetValue(RightProperty, value);
        }
        public static int GetRight(DependencyObject obj)
        {
            return (int)obj.GetValue(RightProperty);
        }
        public static bool IsSetRight(DependencyObject obj)
        {
            return ((int)obj.GetValue(RightProperty) != default(int));
        }

        public Bitmap Draw()
        {
            // FIXME: This should be changed, is temporary
            //MessageBox.Show(Width.ToString() + " " + Height.ToString());
            Bitmap tmp = new Bitmap(Width, Height);
            using (Graphics g = Graphics.FromImage(tmp))
            {
                g.FillRectangle(Brushes.AliceBlue, 0, 0, Width, Height);
                foreach (var item in Items)
                {
                    int x, y;
                    x = y = 0;
                    IDrawable drawableItem = item as IDrawable;
                    DependencyObject dependencyItem = item as DependencyObject;

                    if (dependencyItem != null && drawableItem != null)
                    {
                        if (IsSetTop(dependencyItem))
                            y = GetTop(dependencyItem);
                        else if (IsSetBottom(dependencyItem))
                            y = Height - GetBottom(dependencyItem);

                        if (IsSetLeft(dependencyItem))
                            x = GetLeft(dependencyItem);
                        else if (IsSetRight(dependencyItem))
                            x = Width - GetRight(dependencyItem);
                        g.DrawImage(drawableItem.Draw(), x, y);
                    }
                }
            }
            return tmp;
        }
    }
}
